/********************************** (C) COPYRIGHT *******************************
 * File Name          : Main.c
 * Author             : WCH
 * Version            : V1.0
 * Date               : 2022/03/15
 * Description        : USB IAP APP????
 *********************************************************************************
 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
 * Attention: This software (modified or not) and binary are used for 
 * microcontroller manufactured by Nanjing Qinheng Microelectronics.
 *******************************************************************************/

#include "CH58x_common.h"
#include "app_flag.h"
#include "relay.h"
#include "tim.h"
#include "uart0.h"
//  #include "uart2.h"
// #include "uart3.h"
#include "CH58x_flash.h"
#include "uartapp.h"
#include "basic.h"
#include "iwdg.h"
#include "flash.h"
#include "stdbool.h"
#include "74hc595.h"


/*********************************************************************
 * @fn      DebugInit
 *
 * @brief   ?~{!B~}????????
 *
 * @return  none
 */
void DebugInit(void)
{
    // GPIOA_SetBits(GPIO_Pin_14);
    // GPIOPinRemap(ENABLE, RB_PIN_UART0);
    // GPIOA_ModeCfg(GPIO_Pin_15, GPIO_ModeIN_PU);
    // GPIOA_ModeCfg(GPIO_Pin_14, GPIO_ModeOut_PP_5mA);
    // UART0_DefInit();
     GPIOA_SetBits(bTXD1);
  GPIOA_ModeCfg(bTXD1, GPIO_ModeOut_PP_5mA);
  UART1_DefInit();
}

/*********************************************************************
 * @fn      main
 *
 * @brief   ~{Vw:/J}~}
 *
 * @return  none
 */
int main()
{
    uint16_t i = 0;
    uint8_t  s = 0;
    uint8_t testDate[10]={0,1,2,3,4,5,6,7,8,9};
    HSECFG_Capacitance(HSECap_18p);
    SetSysClock(CLK_SOURCE_HSE_PLL_62_4MHz);
    LClk32K_Select(Clk32K_LSI);
     /* ���ô��ڵ��� */
#ifdef DEBUG 
    DebugInit();
#endif
    PRINT("Start @ChipID=%02x\n", R8_CHIP_ID);
    PRINT("APP  \r\n");
     /* app�������ִ�и���䣬��֤app����ʧ��ʱ���´���Ȼ����IAP */
    SwitchImageFlag(FLAG_USER_CALL_APP);
    USART0_Init(38400);
    TIM1_INT_Init();
    Relay_Init();
    Basic_Init();
    DestMac[5]=KeyScan();
    // Self_Test();
    IWDG_Enable();
    Set_Device_Status(DEVICE_RUNNING);//��������״̬
    Set_Init_Ok_Fg(0);
    Set_Led_Status(false);
    PRINT("run 123456\r\n");
    while (1)
    {
         Uart0_handle();
         Self_Test();
         LED_Handle();
         FEED_IWDG();
         Fire_Scan();
    }
}
